package com.withoutwords.engine.comminication;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

/**
 * 
 * This class is responsible for handling all communication
 * 
 * @author Reda
 * 
 */
public class HttpUtility {
//	IHttpResponseHandler respHandler;
	private HttpUtility()
	{
		// Empty private constructor
	}
	/*public HttpUtility(IHttpResponseHandler handler)
	{
		respHandler = handler;
	}*/
	
	static String readInputStream(InputStream in)
	{
		// String response = "";
		StringBuffer strBuffer = new StringBuffer();
		try {
			if (in != null) {

				/*
				 * byte[] buff; buff = new byte[400]; in.read(buff);
				 * response = new String(buff);
				 */
				char b = 'q';
				int eof = 0;
				do {
					b = (char) in.read();
					eof = b;
					if (eof < 255 && eof > -1) {
						strBuffer.append(b);
					} else {
						break;
					}

				} while (true);

			}
			// return new String(buff);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		return strBuffer.toString();

	}
	/**
	 * Send GET request to server
	 * 
	 * @param url
	 * @param param
	 * @return
	 * @throws IOException
	 */
	private static String sendGetHttpRequest(String url) throws IOException
	{
		
		URL serverURL = null;
		HttpURLConnection conn = null;
		
		// connect to server
		serverURL = new URL(url);
		conn = (HttpURLConnection) serverURL.openConnection();
		conn.setDoInput(true);
		conn.setDoOutput(true);
		conn.setConnectTimeout(100000);
		conn.setReadTimeout(300000);
		conn.setRequestMethod("GET");
		
		conn.connect();
		InputStream in = conn.getInputStream();
		
		String response = readInputStream(in);
		conn.disconnect();
		
		return response;
	}
	
	/**
	 * Send POST request to server
	 * 
	 * @param url
	 * @param param
	 * @return
	 * @throws IOException
	 */
	private static String sendPostHttpRequest(String url, String param) throws IOException
	{
		InputStream in = null;
		URL serverURL = null;
		HttpURLConnection conn = null;
		
		// connect to server
		serverURL = new URL(url);
		conn = (HttpURLConnection) serverURL.openConnection();
		
		// initial Connection
		conn.setDoInput(true);
		conn.setDoOutput(true);
		conn.setRequestMethod("POST");
		conn.setConnectTimeout(2000000);
		conn.setReadTimeout(1000000);
		
		conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		
		DataOutputStream ostream;
		
		// used to remove "?" from request
		//param = param.substring(1);
		
		ostream = new DataOutputStream(conn.getOutputStream());
		
		ostream.write(param.getBytes());
		
		ostream.flush();
		ostream.close();
		
		// Get Request respond
		conn.connect();
		in = conn.getInputStream();
		

		
		String response = readInputStream(in);
		conn.disconnect();
		
		return response;	
	}
	
	/**
	 * Send http request to communication utility
	 * 
	 * @param context
	 * @param url
	 * @param param
	 * @param method
	 * @return input stream
	 */
	public static String sendHttpRequest(//Context context,
			String url,
			String param,
			String method)
	{
		String respond = null;
		
		String requestUrl = "";
		String requestParams = "";
		
		// form request format
		requestUrl = url;
		requestParams = param;
		
		/*if (param != null && param.length() != 0)
		{
			// add basic parameters
			requestParams = "?" + "";
			
			// add request parameters
			requestParams += "&" + param;
		}*/
		// Send POST request
		if (method.equalsIgnoreCase(POST_METHOD))
		{
			try
			{
				
	
				respond = sendPostHttpRequest(requestUrl, requestParams);
			}
			catch (IOException e)
			{
				if(e.getMessage() != null)
				{
				Log.e("sendPostHttpRequest", e.getMessage());
				}
			}
		}
		
		// Send Get request
		else if (method.equalsIgnoreCase(GET_METHOD))
		{
			
			try
			{
				respond = sendGetHttpRequest(requestUrl);
			}
			catch (IOException e)
			{
				if(e.getMessage() != null)
				{
				Log.e("sendGetHttpRequest", e.getMessage());
				}
			}
			
		}
		
		return respond;
	}
	
	// Connection methods
	public static final String POST_METHOD = "POST";
	
	public static final String GET_METHOD = "GET";

/*	@Override
	protected String doInBackground(String... params) {
		InputStream in = sendHttpRequest(params[0], params[1], params[2]);
		byte[] buff;
		try {
			buff = new byte[in.available()];
			in.read(buff);
			return new String(buff);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
		
	}
	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		if(respHandler != null)
			respHandler.handleHttpResponse(result);
		super.onPostExecute(result);
	}
	*/
}
